R 您所在的位置:网站首页 stringr r install R

R

2023-03-06 22:17| 来源: 网络整理| 查看: 265

本文简单记录R包开发的流程

准备

创建R包需要用到 devtools,以及usethis

install.packages(c("devtools","usethis")) library(devtools) library(usethis) 创建Git repo (可选)

如果需要使用Git管理R包,我们先要在GitHub上创建该包的Git repo,

在GitHub登陆后,进入[Your Repositories] → [New]

创建一个叫"regexcite"的库进行练习

然后,clone这个仓库到本地,再开始搭建我们的包

git clone https://github.com/thereallda/regexcite.git

注意,这里要将https://github.com/thereallda/regexcite.git换成你创建的地址。

初始化R包

打开RStudio,使用函数 usethis::create_package() 初始化克隆到本地电脑的包的目录 (最好是绝对路径)

usethis::create_package("~/path/to/regexcite")

使用后在目录("~/path/to/regexcite")下创建以下文件和目录:

.Rbuildignore* .Rhistory* .Rproj.user/ .gitignore* DESCRIPTION* NAMESPACE* R/ regexcite.Rproj*

.Rbuildignore lists files that we need to have around but that should not be included when building the R package from source.

.Rproj.user, if you have it, is a directory used internally by RStudio.

.gitignore anticipates Git usage and ignores some standard, behind-the-scenes files created by R and RStudio. Even if you do not plan to use Git, this is harmless.

DESCRIPTION provides metadata about your package. We edit this shortly.

NAMESPACE declares the functions your package exports for external use and the external functions your package imports from other packages. At this point, it is empty, except for a comment declaring that this is a file we will not edit by hand.

The R/ directory is the "business end" of your package. It will soon contain .R files with function definitions.

regexcite.Rproj is the file that makes this directory an RStudio Project. Even if you don't use RStudio, this file is harmless. Or you can suppress its creation with create_package(..., rstudio = FALSE).

创建第一个函数

函数的脚本应当存放在 R/ 目录下。可以直接创建脚本保存于其中。也可以使用函数 use_r() 创建

use_r("strsplit1")

use_r("strsplit1") 直接创建"strsplit1.R"到 R/ 下

我们写下第一个函数 strsplit1 是对 base::strsplit() 的包装,原函数返回一个 list ,而 strsplit1 只取返回结果的第一个元素,相当于是 unlist(strsplit(x, split)) .

# split a single string strsplit1 use_mit_license() √ Setting License field in DESCRIPTION to 'MIT + file LICENSE' √ Writing 'LICENSE' √ Writing 'LICENSE.md' √ Adding '^LICENSE\\.md$' to '.Rbuildignore'

LICENSE 文件内应该是:

YEAR: 2022 COPYRIGHT HOLDER: regexcite authors 修改 DESCRIPTION

打开 DESCRIPTION 文件进行修改,里面有一些样式的内容,主要改一下 Authors@R 和 Description 区域

改好之后是这样

Package: regexcite Title: Toy package for paracticing the process about developing R package. Version: 0.0.0.9000 Authors@R: person(given = "Dean", family = "Li", role = c("aut", "cre"), email = "") Description: Convenience functions to make some common tasks with string manipulation and regular expressions a bit easier. License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a license Encoding: UTF-8 LazyData: true Roxygen: list(markdown = TRUE) RoxygenNote: 7.1.1 添加说明文档

通过 roxygen2 注释系统在脚本内部写下函数有关的文档。

在RStudio内,移动光标到函数的代码区段,点击 Code > Insert Roxygen Skeleton 将会自动添加 #' 开头的 roxygen2 注释

修改这段注释,在相应区域内写上对应的描述即可

#' Split a string #' #' @param x A character vector with one element. #' @param split What to split on. #' #' @return A character vector. #' @export #' #' @examples #' x


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有